package cn.com.nxt.txb;

import android.content.Intent;
import android.os.Handler;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.AuthService;
import com.netease.nimlib.sdk.auth.LoginInfo;

import cn.com.nxt.txb.education.util.Preferences;
import cn.com.nxt.txb.util.AppUtil;
import cn.com.nxt.txb.util.AuthPreferences;
import cn.com.nxt.txb.util.UserPreferences;

public class LeadActivity extends BaseActivity {
    private String account, token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lead);
        new Handler().postDelayed(new Runnable() {
            public void run() {
                Class cls = LoginActivity.class;
                boolean isFirst = sp.getBoolean(AppUtil.SETTING_ISFIRST, true);
                if (isFirst)
                    cls = GuideActivity.class;
                else if (canAutoLogin()) {
                    AppUtil.mUser = AppUtil.readUser(LeadActivity.this);
                    //cls = MainActivity.class;
                    doLogin(account,token);
                    return;
                }
                startActivity(new Intent(LeadActivity.this, cls));
                finish();
            }
        }, 2000);
    }

    public void doLogin(final String account, final String token) {
        LoginInfo info = new LoginInfo(account, token); // config...
        RequestCallback<LoginInfo> callback =
                new RequestCallback<LoginInfo>() {
                    @Override
                    public void onSuccess(LoginInfo loginInfo) {
                        //startActivity(new Intent(LeadActivity.this, MQTTService.class));
                        // Log.i("--LOGIN--", "login success");
                        //showToast("--LOGIN--"+account);
                        //Log.e("--LOGIN--", AppUtil.mUser.getPhone());
                        //SampleApplicationLike.mqttBinder.subscribe("/meetting/invlite/:"+AppUtil.mUser.getPhone());
                        DemoCache.setAccount(account);
                        // saveLoginInfo(account, token);
                        // 初始化消息提醒
                        NIMClient.toggleNotification(UserPreferences.getNotificationToggle());
                        // 初始化免打扰
                        NIMClient.updateStatusBarNotificationConfig(UserPreferences.getStatusConfig());
                        // 进入主界面
                        startActivity(new Intent(LeadActivity.this, MainActivity.class));
                        finish();
                    }

                    @Override
                    public void onFailed(int code) {
                        if (code == 302 || code == 404) {
                            Toast.makeText(LeadActivity.this, R.string.login_failed, Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(LeadActivity.this, "登录失败: " + code, Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onException(Throwable throwable) {

                    }
                    // 可以在此保存LoginInfo到本地，下次启动APP做自动登录用
                };
        NIMClient.getService(AuthService.class).login(info)
                .setCallback(callback);
    }


    /**
     * 已经登陆过，自动登陆
     */
    private boolean canAutoLogin() {
       account = AuthPreferences.getUserAccount();
       token = AuthPreferences.getUserToken();
        // Log.i(TAG, "get local sdk token =" + token);
        return !TextUtils.isEmpty(account) && !TextUtils.isEmpty(token);
    }
}
